package com.ytjj.qmyx.mall.controller.h5;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.model.response.ActivityProductResponse;
import com.ytjj.qmyx.mall.model.response.H5SkillIndexResponse;
import com.ytjj.qmyx.mall.model.response.H5SkillPageResponse;
import com.ytjj.qmyx.mall.service.ActivityService;
import com.ytjj.qmyx.mall.service.h5.H5SkillService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/h5/skill")
public class H5SkillActivityController {

    @Resource
    private H5SkillService h5SkillService;

    @Resource
    private ActivityService activityService;

    @Resource
    private RedisService redisService;

    /**
     * h5 秒杀页
     */
    @GetMapping("get")
    public CommonResult getSkillPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize",required = false, defaultValue = "20") Integer pageSize) {
        H5SkillPageResponse response = h5SkillService.getSkillPage(pageNum,pageSize);
        return CommonResult.success(response);
    }

    /**
     * 秒杀首页
     * @return
     */
    @GetMapping("index")
    public CommonResult index() {
        String key = com.ytjj.qmyx.mall.constants.RedisConstants.ACTIVITY_PRODUCT + ActivityTypeEnums.SECKILL.name() + TokenUtil.getDataType() + "_H5_index";
        String s = redisService.get(key);
        H5SkillIndexResponse h5SkillIndexResponse=new H5SkillIndexResponse();
        if (StringUtils.isNotEmpty(s)) {
            h5SkillIndexResponse= JSONObject.parseObject(s, H5SkillIndexResponse.class);
        }else {
            List<ActivityProductResponse> goldProductList = activityService.getActivityList(1, 9, ActivityTypeEnums.CHINA_GOLD.name(), 0);
            h5SkillIndexResponse.setGoldProductList(goldProductList);
            h5SkillIndexResponse.setRankingProductList(h5SkillService.getSkillList(73, 1, 3));
            List<ActivityProductResponse> superProductList = new ArrayList<>();
            List<ActivityProductResponse> superNoneStockProductList = h5SkillService.getSkillList(72, 1, 9);
            if (superNoneStockProductList.size() > 0) {
                superProductList.add(superNoneStockProductList.get(new Random().nextInt(superNoneStockProductList.size())));
            }
            superProductList.addAll(h5SkillService.getSkillList(71, 1, 5));
            h5SkillIndexResponse.setSuperProductList(superProductList);
            h5SkillIndexResponse.setShoppingCardProductList(activityService.getActivityList(1, 3,ActivityTypeEnums.SHOPPING_CARD.name(),0));
            redisService.set(key, JSONObject.toJSONString(h5SkillIndexResponse));
            redisService.expire(key, 60);
        }
        return CommonResult.success(h5SkillIndexResponse);
    }

    /**
     * 栏目商品列表
     *
     * @return
     */
    @GetMapping("/queryListByPage")
    public CommonResult queryListByPage(@RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                        @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag) {

        List<ActivityProductResponse> productList = h5SkillService.getSkillList(columnId,pageNum, pageSize);
        if(1==flag){//打乱顺序
            Collections.shuffle(productList);
        }
        return CommonResult.success(productList);
    }
}
